{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 向量化后的SimpleLinearRegression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = np.array([1., 2., 3., 4., 5.])\n",
    "y = np.array([1., 3., 2., 3., 5.])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SimpleLinearRegression()"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from playML.SimpleLinearRegression import SimpleLinearRegression\n",
    "regression = SimpleLinearRegression()\n",
    "regression.fit(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.80000000000000004"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regression.a_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.39999999999999947"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regression.b_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAD8CAYAAABXe05zAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGDJJREFUeJzt3XuclGX9xvHPV1h1FXVTVxOQULRF0pBcNcPwlK4aKZXl\nIU3NolJLMVfFY0AJhObZlBAPoZAi4CnBYyop6C6gi8gSkAqLBmirgAsuy/f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1t17J50nk8zsTaDc\n3fNy8Y2Z3Q284O4jUrP3tnL3+vVdn8k77rxeGu/uzwPvJ50jW9z9HXeflvp6GfAGYdVsXvBgeerb\notRfefNk3sw6At8FRiSdRVrGzLYDegF3ALj7xxsqbchsca9raXze/OAXEjPrDPQApiabJLNSQwkz\ngMXAk+6eT+/veuAiYE3SQbLEgafMrDq1vUY+2Q1YAtyZGuoaYWZbb+gX6OGkfI6ZtQMeBM539w+T\nzpNJ7t7k7vsSVv8eYGZ5MeRlZr2Bxe5enXSWLDo49dkdA5yTGrrMF22BbwB/cfcewApgg88IM1nc\nWhofudTY74PAve4+Luk82ZL6z9BngaOTzpIhPYHjUuPAY4DDzWxUspEyy93rUn9fDIwnDM3mi4XA\nws/8F+BYQpGvVyaLW0vjI5Z6eHcH8Ia7/znpPJlmZqVmVpL6upjwEH12sqkyw937u3tHd+9M+Ll7\nxt1PTThWxpjZ1qkH5qSGEI4C8mZ2l7u/Cywws7W7Ax4BbHBSQCZ3B9yYpfHRMLPRwKHAjma2ELjK\n3e9INlVG9QROA2pS48AAl7r7PxLMlEm7AHenZj9tBtzv7nk3bS5P7QyMD/cWtAXuc/eJyUbKuN8A\n96ZueucDZ27oYi15FxGJjB5OiohERsUtIhIZFbeISGRU3CIikVFxi4hERsUtIhIZFbeISGT+D6TJ\nLLSYywanAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x116639860>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "y_predict = regression.predict(x)\n",
    "plt.scatter(x, y)\n",
    "plt.plot(x, y_predict, color=\"r\")\n",
    "plt.axis([0,6,0,6])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "m = 1000000\n",
    "big_x = np.random.random(size=m)\n",
    "big_y = big_x*2.0 + 3.0 + np.random.normal(size=m) # 最后加个随机干扰噪声"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 loops, best of 3: 19.3 ms per loop\n"
     ]
    }
   ],
   "source": [
    "%timeit regression.fit(big_x, big_y) # 前面第3节实现的for循环来的方式时间是900多ms，速度差接近50倍，my god,向量点乘快这么多！！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
